package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.View;


@Repository
public class ViewDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	
	public void addView(View view) {
		try{
		sessionFactory.getCurrentSession().save(view);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public List<View> getView(Integer viewOwnerId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from View where  viewOwner.id = :viewOwnerId");
		query.setParameter("viewOwnerId", viewOwnerId);
		@SuppressWarnings("unchecked")
		List<View>  views= query.list();
		if(views != null && views.size() > 0)
		return views;
		else
			return new ArrayList<View>();
	}
	public View getViewById(Integer Id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from View where  id = :Id");
		query.setParameter("Id", Id);
		@SuppressWarnings("unchecked")
		List<View>  views= query.list();
		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+views.size());
		if(views.size() > 0)
		return views.get(0);
		else
		return null;
	}

	public void saveOrupdate(View view){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(view);
	}
	
	public View loadView(Integer id) {
		View view = (View)sessionFactory.getCurrentSession().get(View.class, id);
		return view;
		
	}
	
	public void deleteView(View view) {
		sessionFactory.getCurrentSession().delete(view);
		
	}

}
